﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;


namespace RFW.Weather
{
	public class Icons
	{
		private Dictionary<string, string> _icons = null;

		public string this[string name]
		{
			get
			{
				string uri = string.Empty;
				if (_icons != null && _icons.ContainsKey(name))
					uri = _icons[name];

				return uri;
			}
		}

		public string this[string name, string skyIcon]
		{
			get
			{
				string uri = this[name];
				
				int pos = uri.LastIndexOf("/");
				if (pos > 0)
					uri = uri.Substring(0, pos + 1) + skyIcon + ".gif";

				System.Diagnostics.Debug.WriteLine(uri);

				return uri;
			}
		}

		internal void Process(XElement element)
		{
			if (_icons != null)
			{
				_icons.Clear();
				_icons = null;
			}

			_icons = new Dictionary<string, string>();

			foreach (XElement elm in element.Elements("icon_set"))
				_icons.Add(elm.Attribute("name").Value, elm.Element("icon_url").Value);
		}

		public static Icons Linq(XElement element)
		{
			Icons icons = new Icons();
			icons.Process(element);

			return icons;
		}
	}
}
